import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESCipher {
    private static final String ALGORITHM = "AES";
    //密钥长度可以设置128，192，256,默认128
    private int KEY_SIZE = 128;

    void setKEY_SIZE(int keySize){
        KEY_SIZE = keySize;
    }

    byte[] encrypt(byte[] key, byte[] input) throws Exception{
        byte[] keyBytes = key;
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, 0 , KEY_SIZE/8, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(input);
    }

    byte [] decrypt(byte[] key, byte[]input) throws Exception{
        byte[] keyBytes = key;
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, 0 , KEY_SIZE/8, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return cipher.doFinal(input);
    }
}
